import icon from "!css-loader?modules=true!element-ui/packages/theme-chalk/lib/icon.css";
const iconMap = icon.locals;
const matches = {};
icon
  .toString()
  .match(/\.[\w\_\-]+\:before{([\s\S]*?)}/g)
  .reduce((prev, current) => {
    const key = current.match(/(?<=\.)[\w\_\-]+(?=\:)/)?.pop();
    const value = current.match(/(?<=content\:"\\)[\s\S]+(?=\")/)?.pop();
    if (key && value) {
      prev[key] = {
        origin: `\\${value}`,
        decoded: String.fromCharCode(parseInt(value, 16)),
      };
    }
    return prev;
  }, matches);
Object.entries(iconMap).forEach(([key, value]) => {
  if (matches[value]) {
    iconMap[key] = matches[value];
  } else {
    delete iconMap[key];
  }
});
export default iconMap;

let loaded = false;
export const elementLoaded = () => {
  if (loaded) return Promise.resolve(true);

  const check = () => {
    const cvs = document.createElement("canvas");
    cvs.width = 80;
    cvs.height = 80;
    const ctx = cvs.getContext("2d");
    ctx.font = "70px element-ui";
    ctx.fillStyle = "red";
    console.log(iconMap["el-icon-eleme"].decoded);
    ctx.fillText(iconMap["el-icon-eleme"].decoded, 0, 0);
    const p = ctx.getImageData(0, 0, 80, 80);
    p.data.forEach((i) => {
      if (i !== 0) {
        console.log(i);
      }
    });
    return false;
  };
  return new Promise((resolve, reject) => {
    let count = 0;
    const timer = setInterval(() => {
      if (loaded || check()) {
        clearInterval(timer);
        resolve(true);
        return;
      }
      if (count > 15) {
        clearInterval(timer);
        reject();
        return;
      }
      count++;
    }, 1000);
  });
};
